<?php

namespace backend\controllers;

use bc\controller\BackendController;
use bc\model\ProductJpgModel;
use bc\model\ProductModel;
use bc\model\ProductsampleModel;
use bc\util\backend\Page;

class ProductController extends BackendController
{
	
	/**
	 * 
	 */
	public function actionIndex()
	{
		$productModel = new ProductModel();
		$row = $productModel->getRowById(3);
                
                
	}
        
        public function actionShowlist()
        {
                $productModel = new ProductModel();
                $list = $productModel->getProductInfo();               
                //var_dump($list);die();
                foreach($list as $k => $v){
                    $productjpgM = new ProductJpgModel();
                    $productjpgM->autoId='jpg_id';
                    $productjpgD = $productjpgM->getList(['product_id'=>$v['product_id']]);
                    $list[$k]['jpg']=$productjpgD;
                }
                $total = $productModel->getTotal('');
                $pagination = new Page($total, self::PAGE_SIZE);
//                print_r($list);
//            die();
                return  $this->render('showlist',array('list'=>$list, 'pagination' => $pagination ));
        }
	
	
	/**
	 * 
	 */
	public function actionAdd()
	{
		
		return $this->render('add');
	}
		
	public function actionEdit()
	{       
            
                           $id = $this->get('id');
                           //var_dump($id);die();
                           if(!empty($_POST)){
                                    
                                    //修改product表信息
                                    $productModel = new ProductModel();
                                    $info['product_name']=  $this->post('product_name');
                                    $info['short_description']=  $this->post('short_description');
                                    $info['market_price'] = $this->post('market_price');
                                    $info['product_price'] = $this->post('product_price');
                                    $info['description']  =  !empty($_POST['description']) ? $_POST['description']:'' ;
                                    $info['upate_time'] = time();

                                    $productModel->modify($info,$id);                                       
                                   
                                    //修改product_sample表信息
                                    $productsampleModel = new ProductsampleModel();
                                    $data['home_html'] = preg_replace('/\<[p\/\s\t\n\<\>]+[\>\s\t\n]$/','',preg_replace('/^[\<\s\t\n]+[p\/\s\t\n\<\>]+\>/','',$this->post('home_html')));
                                    $data['bind_img'] = !empty($_POST['bind_img']) ? preg_replace('/\<[p\/\s\t\n\<\>]+[\>\s\t\n]$/','',preg_replace('/^[\<\s\t\n]+[p\/\s\t\n\<\>]+\>/','',$this->post('bind_img'))):"";
                                    $data['bind_html'] = !empty($_POST['bind_html']) ? preg_replace('/\<[p\/\s\t\n\<\>]+[\>\s\t\n]$/','',preg_replace('/^[\<\s\t\n]+[p\/\s\t\n\<\>]+\>/','',$this->post('bind_html'))):"";
                                    $data['product_img'] = preg_replace('/\<[p\/\s\t\n\<\>]+[\>\s\t\n]$/','',preg_replace('/^[\<\s\t\n]+[p\/\s\t\n\<\>]+\>/','',$this->post('product_img')));
                                    $data['bind_check_length'] = !empty($_POST['bind_check_length']) ? preg_replace('/\<[p\/\s\t\n\<\>]+[\>\s\t\n]$/','',preg_replace('/^[\<\s\t\n]+[p\/\s\t\n\<\>]+\>/','',$this->post('bind_check_length'))):"";
                                    $data['item_title'] = preg_replace('/\<[p\/\s\t\n\<\>]+[\>\s\t\n]$/','',preg_replace('/^[\<\s\t\n]+[p\/\s\t\n\<\>]+\>/','',$this->post('product_name')));
                                    $data['item_success_html'] =!empty($_POST['item_success_html']) ? preg_replace('/\<[p\/\s\t\n\<\>]+[\>\s\t\n]$/','',preg_replace('/^[\<\s\t\n]+[p\/\s\t\n\<\>]+\>/','',$this->post('item_success_html'))):"";
                                    $data['pay_success_html'] = preg_replace('/\<[p\/\s\t\n\<\>]+[\>\s\t\n]$/','',preg_replace('/^[\<\s\t\n]+[p\/\s\t\n\<\>]+\>/','',$this->post('pay_success_html')));
                                    $data['code'] = preg_replace('/\<[p\/\s\t\n\<\>]+[\>\s\t\n]$/','',preg_replace('/^[\<\s\t\n]+[p\/\s\t\n\<\>]+\>/','',$this->post('code')));
                                    $data['upate_time'] = time();
                                    $primaryIds = $productsampleModel->getPrimaryKeyByProductId($id);
                                    $primaryId = implode('',$primaryIds);
                                   
                                    if(!empty($primaryId))
                                    {       
                                            
                                            $productsampleModel->modify($data,$primaryId);                                       
                                          
                                    }
                                    else 
                                    {
                                            $data['create_time'] = time();
                                            $productsampleModel->add($info);
                                    }

                                    $output['msg'] = '操作成功!';
                                    $output['url'] = '/product/showlist';
                                    $this->forward($output);

                          }
		
		$productModel = new ProductModel();
		$row = $productModel->getRowByIds($id);
		
                $productsampleModel = new ProductsampleModel();
                $list =$productsampleModel->getListById($id);
                //var_dump($list);die();
		if(!$id || !$row || !$list)
			$this->redirect('/');
                    //var_dump($row);die();
		//$view['row'] = $row;
               // $view['list'] = $list;
                //var_dump($row);die();
		return $this->render('edit',array('row' => $row, 'list' => $list));
		
	}
	
	
	/**
	 * 数据保存
	 */
	public function actionSave()
	{      
             

             if(!empty($_POST)){
                		
		$productModel = new ProductModel();
		
                $data['category_id'] = $this->post('cate_id','');
		$data['product_name'] = $this->post('product_name', '');
                $data['short_description']=  $this->post('short_description');
		$data['market_price'] = $this->post('market_price');
		$data['product_price'] = $this->post('product_price');
		$data['description']  =  !empty($_POST['description']) ? $_POST['description']:'' ;
		$data['is_sale'] = 1;		
		$data['upate_time'] = time();
                

                $data['create_time'] = time();
	        $proId = $productModel->add($data);
	
                //保存product_sample表
                
                $productsampleModel = new ProductsampleModel();
                $info['product_id'] = $proId;
                $info['home_html'] = $this->post('home_html');
                $info['bind_img'] = !empty($_POST['bind_img']) ? $_POST['bind_img']:"";
                $info['product_img'] = $this->post('product_img');
                $info['bind_html'] = !empty($_POST['bind_html']) ? $_POST['bind_html']:"";
                $info['bind_check_length'] = !empty($_POST['bind_check_length']) ? $_POST['bind_check_length']:"0";
                $info['item_title'] = $this->post('product_name');
                $info['item_success_html'] = !empty($_POST['item_success_html']) ? $_POST['item_success_html']:"";
                $info['pay_success_html'] = $this->post('pay_success_html');
                $info['code'] = $this->post('code');
               
		$info['create_time'] = time();
		$productsampleModel->add($info);
                                             
		$output['msg'] = '操作成功!';
		$output['url'] = '/product/showlist';
		$this->forward($output);
                		
	   }
           $cate_id = $_GET['cate_id'];
           return  $this->render('save',array('cate_id' => $cate_id));
        }
        
        public function  actionDelete(){
                $id = $this->get('id');
                $productModel = new ProductModel();
                $row = $productModel->getRowById($id);
                if($row['is_sale'] == 1){
                    $row['is_sale'] = 2; 
                    $productModel->modify($row, $id);
                }elseif($row['is_sale'] == 2){                  
                    $row['is_sale'] = 1; 
                    $productModel->modify($row, $id);
                    
                }
                
                $output['msg'] = '操作成功!';
		$output['url'] = '/product/showlist';
		$this->forward($output);
                
        }
        
}

